#include "myDevice.h"
#include "stm32f1xx_hal.h"
#include <stdio.h>
#include "myUart.h"
#include "myFlash.h"
#include "tim.h"
#include "adc.h"
#include "crc.h"
/***************************** ADC 电流采样*************************/
int16_t tmp_adcData[adc_num] = {0};
adcValue_type adcValue;
void ADC_dispose(void)
{
     adcValue.value1 = adcValue.value2 = adcValue.value3 = adcValue.value4 = 0;
     HAL_ADC_Start_DMA(&hadc1, (uint32_t *)tmp_adcData, adc_num); // 因为你选择的软件触发，所以每次采集都需要开启一次
     static uint8_t i;
     for (i = 1; i <= 8; i++) //// 遍历10次，进行滤波,第一个和最后一个舍弃
     {
          adcValue.value1 += tmp_adcData[0 + 4 * i];
          adcValue.value2 += tmp_adcData[1 + 4 * i];
          adcValue.value3 += tmp_adcData[2 + 4 * i];
          adcValue.value4 += tmp_adcData[3 + 4 * i];
     }
     adcValue.value1 = adcValue.value1 / 8;
     adcValue.value2 = adcValue.value2 / 8;
     adcValue.value3 = adcValue.value3 / 8;
     adcValue.value4 = adcValue.value4 / 8;
     // printf("ADC为%d,%d,%d,%d",adcValue.value1,adcValue.value2,adcValue.value3,adcValue.value4);
}
/*****************************输出控制*************************/
void pwmControl(u8 *buf)
{
     u8 id = buf[0];
     switch (id)
     {
     case 1:
          __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_2, buf[1] * 5);
          printf("第1路pwm设置为%d", buf[1]);
          break;
     case 2:
          __HAL_TIM_SetCompare(&htim2, TIM_CHANNEL_1, buf[1] * 5);
          printf("第2路pwm设置为%d", buf[1]);
          break;
     case 3:
          __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_4, buf[1] * 5);
          printf("第3路pwm设置为%d", buf[1]);
          break;
     case 4:
          __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_3, buf[1] * 5);
          printf("第4路pwm设置为%d", buf[1]);
          break;
     case 5:
          __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_2, buf[1] * 5);
          printf("第5路pwm设置为%d", buf[1]);
          break;
     case 6:
          __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, buf[1] * 5);
          printf("第6路pwm设置为%d", buf[1]);
          break;
     }
}
void motorControl(u8 *buf)
{
     u8 mn = buf[0];  // 电机号
     u8 dir = buf[1]; // 方向
     u8 pwm = buf[2]; // 速度占空比(0-100)
     if (dir == 0)
     {
          pwm = 0;
          dir++; // 便于接下来将占空比设置为0
     }
     switch (mn)
     {
     case 0x01:
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, (--dir) ? GPIO_PIN_RESET : GPIO_PIN_SET);
          if (dir)
               __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_2, pwm * 5);
          else
               __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_2, (100 - pwm) * 5);

          break;
     case 0x02:
          HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, (--dir) ? GPIO_PIN_RESET : GPIO_PIN_SET);
          if (dir)
               __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, pwm * 5);
          else
               __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, (100 - pwm) * 5);
          break;
     }
}
/*****************************输入反馈*************************/
void Input_Feedback(void)
{
     u8 buf[10] = {0x44, 0x01, 0x00, 0x0A, 0x42, 0x00, 0x00, 0x00, 0x00, 0xDA};
     u8 Input[12] = {0};
     u16 data0 = 0;
     u16 data1 = 0;

     Input[0] = !HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_2);
     Input[1] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_12);
     Input[2] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_11);
     Input[3] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_10);
     Input[4] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_9);
     Input[5] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_8);
     Input[6] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);
     Input[7] = !HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);
     Input[8] = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_15);
     Input[9] = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_14);
     Input[10] = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_13);
     Input[11] = !HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12);

     for (u8 i = 0; i < 8; i++)
          data0 |= Input[i] << (7 - i);

     for (int i = 8; i < 12; i++)
          data1 |= Input[i] << (15 - i);

     buf[6] = data0;
     buf[7] = data1;
     buf[8] = SumCheck(buf, sizeof(buf) - 2);
     HAL_UART_Transmit(&huart2, buf, sizeof(buf), 255);
}
